﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace VietImage.Models
{
    public partial class Lightbox
    {
        #region Properties
        public Status Status
        {
            get
            {
                return this.GetStatus();
            }
        }

        public User User
        {
            get
            {
                return this.GetUser();
            }
        }

        public List<Image> Images
        {
            get
            {
                return this.GetListAvailableImage();
            }
        }
        #endregion

        #region Methods
        public Status GetStatus()
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = from Status in _db.Status
                    where Status.ID == this.ID_Status
                    select Status;
            if (v.Count() == 0)
                return null;
            return v.First();
        }

        public List<ErrorItem> InsertImage(int id_Image, int id_User, int id_Lang)
        {
            List<ErrorItem> errorList = new List<ErrorItem>();

            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();

            int errorMess = _db.InsertImageToLightbox(id_User, id_Image, this.ID, 0, 0);


            if(errorMess != 1) errorList.Add(new ErrorItem("ErrorMessage", _db.ErrorMessages.Single(p => p.ID_ErrorMessage == errorMess && p.ID_Language == id_Lang).Description));

            return errorList;
        }

        public List<ErrorItem> InsertImage(int id_Image, int id_User, int id_Lang, int id_Log)
        {
            List<ErrorItem> errorList = new List<ErrorItem>();

            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();

            int errorMess = _db.InsertImageToLightbox(id_User, id_Image, this.ID, id_Log, 1);


            if(errorMess != 1) errorList.Add(new ErrorItem("ErrorMessage", _db.ErrorMessages.Single(p => p.ID_ErrorMessage == errorMess && p.ID_Language == id_Lang).Description));

            return errorList;
        }

        public List<ErrorItem> RemoveImage(int id_Image, int id_User, int id_Lang)
        {
            List<ErrorItem> errorList = new List<ErrorItem>();

            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();

            int errorMess = _db.RemoveImageFromLightbox(id_User, id_Image, this.ID, 0, 0);

            if(errorMess != 1) errorList.Add(new ErrorItem("ErrorMessage", _db.ErrorMessages.Single(p => p.ID_ErrorMessage == errorMess && p.ID_Language == id_Lang).Description));

            return errorList;
        }

        public List<ErrorItem> RemoveImage(int id_Image, int id_User, int id_Lang, int id_Log)
        {
            List<ErrorItem> errorList = new List<ErrorItem>();

            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();

            int errorMess = _db.RemoveImageFromLightbox(id_User, id_Image, this.ID, id_Log, 1);

            if(errorMess != 1) errorList.Add(new ErrorItem("ErrorMessage", _db.ErrorMessages.Single(p => p.ID_ErrorMessage == errorMess && p.ID_Language == id_Lang).Description));

            return errorList;
        }

        public List<ErrorItem> SetPrimaryImage(System.Data.Linq.Binary image, int id_User, int id_Lang)
        {
            List<ErrorItem> errorList = new List<ErrorItem>();

            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();

            int errorMess = _db.ChangePrimaryImageForLightbox(id_User, image, this.PrimaryImage, this.ID, 0, 0);


            if(errorMess != 1) errorList.Add(new ErrorItem("ErrorMessage", _db.ErrorMessages.Single(p => p.ID_ErrorMessage == errorMess && p.ID_Language == id_Lang).Description));

            return errorList;
        }

        public List<ErrorItem> SetPrimaryImage(System.Data.Linq.Binary image, int id_User, int id_Lang, int id_Log)
        {
            List<ErrorItem> errorList = new List<ErrorItem>();

            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();

            int errorMess = _db.ChangePrimaryImageForLightbox(id_User, image, null, this.ID, id_Log, 1);


            if(errorMess != 1) errorList.Add(new ErrorItem("ErrorMessage", _db.ErrorMessages.Single(p => p.ID_ErrorMessage == errorMess && p.ID_Language == id_Lang).Description));

            return errorList;
        }

        public User GetUser()
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = from users in _db.Users
                    where users.ID == this.ID_User
                    select users;
            if (v.Count() == 0)
                return null;
            return v.First();
        }


        public List<Image> GetListAvailableImage()
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = from images in _db.Images
                       join imgcate in _db.ImageLightboxes on images.ID equals imgcate.ID_Image
                       where imgcate.ID_Lightbox == this.ID && images.ID_Status != 2 && imgcate.ID_Status != 8
                    select images;
            if (v.Count() == 0)
                return null;
            return v.ToList();
        }

        public List<Image> GetListNotAvailableImage()
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = from images in _db.Images
                       join imgcate in _db.ImageLightboxes on images.ID equals imgcate.ID_Image
                       where imgcate.ID_Lightbox == this.ID && images.ID_Status == 2 && imgcate.ID_Status != 8
                    select images;
            if (v.Count() == 0)
                return null;
            return v.ToList();
        }

        public List<Image> GetListRemovedAvailableImage()
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = from images in _db.Images
                       join imgcate in _db.ImageLightboxes on images.ID equals imgcate.ID_Image
                       where imgcate.ID_Lightbox == this.ID && images.ID_Status != 2 && imgcate.ID_Status == 8
                    select images;
            if (v.Count() == 0)
                return null;
            return v.ToList();
        }

        public List<Image> GetListRemovedNotAvailableImage()
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = from images in _db.Images
                       join imgcate in _db.ImageLightboxes on images.ID equals imgcate.ID_Image
                       where imgcate.ID_Lightbox == this.ID && images.ID_Status == 2 && imgcate.ID_Status == 8
                    select images;
            if (v.Count() == 0)
                return null;
            return v.ToList();
        }
        #endregion

        #region Static Methods
        public static Lightbox GetLightbox(int id)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = _db.Lightboxes.Where(p => p.ID == id);
            if (v.Count() == 0)
                return null;
            return v.First();
        }

        public static Lightbox Parse(System.Object value)
        {
            Lightbox temp = null;
            try
            {
                temp = (Lightbox)value;
            }
            catch (Exception ex)
            {
            }
            return temp;
        }

        public static List<Lightbox> GetList()
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = from Lightboxes in _db.Lightboxes
                    select Lightboxes;
            if (v.Count() == 0)
                return null;
            return v.ToList();
        }

        public static List<Lightbox> GetListAvailable()
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = _db.Lightboxes.Where(p => p.ID_Status != 2);
            if (v.Count() == 0)
                return null;
            return v.ToList();
        }

        public static List<Lightbox> GetListAvailableOfUser(int id_User)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = _db.Lightboxes.Where(p => p.ID_Status != 2 && p.ID_User == id_User);
            if (v.Count() == 0)
                return null;
            return v.ToList();
        }

        public static List<Lightbox> GetListNotAvailable()
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = _db.Lightboxes.Where(p => p.ID_Status == 2);
            if (v.Count() == 0)
                return null;
            return v.ToList();
        }

        private static List<ErrorItem> InputInfomationValidation(Lightbox lightbox)
        {
            List<ErrorItem> errorList = new List<ErrorItem>();
            if (lightbox.Name == "")
                errorList.Add(new ErrorItem("Name", "Lightbox Name is required."));
            if (lightbox.Name.Length > 50)
                errorList.Add(new ErrorItem("Name", "Lightbox Name is only allow up to 50 characters."));
            if (lightbox.ID_User < 1)
                errorList.Add(new ErrorItem("ID_User", "User is required"));
            if (lightbox.ID_Status < 1)
                errorList.Add(new ErrorItem("ID_Status", "ID_Status is required"));
            return errorList;
        }

        public static List<ErrorItem> Insert(ref Lightbox ii, int id_User, int id_Lang)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            List<ErrorItem> errorList = InputInfomationValidation(ii);
            if (errorList.Count == 0)
            {
                Nullable<Int32> id_Lightbox = new int();
                int errorMess = _db.InsertLightbox(ref id_Lightbox, ii.Name, id_User);
                if (errorMess == 1)
                    ii.ID = id_Lightbox.Value;

                if(errorMess != 1) errorList.Add(new ErrorItem("ErrorMessage", _db.ErrorMessages.Single(p => p.ID_ErrorMessage == errorMess && p.ID_Language == id_Lang).Description));
            }
            return errorList;
        }

        public static List<ErrorItem> ChangeInformation(Lightbox ic, int id_User, int id_Lang)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            List<ErrorItem> errorList = InputInfomationValidation(ic);
            if (errorList.Count == 0)
            {
                Lightbox oi = _db.Lightboxes.Single(p => p.ID == ic.ID);
                String content = "";
                content = oi.Name + "_" + oi.ID_User + "_" + oi.ID_Status;
                int errorMess = _db.ChangeLightbox(ic.ID, ic.Name, id_User, ic.ID_Status, content, 0, 0);

                if(errorMess != 1) errorList.Add(new ErrorItem("ErrorMessage", _db.ErrorMessages.Single(p => p.ID_ErrorMessage == errorMess && p.ID_Language == id_Lang).Description));
            }
            return errorList;
        }

        public static List<ErrorItem> ChangeInformation(Lightbox ic, int id_User, int id_Log, int id_Lang)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            List<ErrorItem> errorList = InputInfomationValidation(ic);
            if (errorList.Count == 0)
            {
                Lightbox oi = _db.Lightboxes.Single(p => p.ID == ic.ID);
                int errorMess = _db.ChangeLightbox(ic.ID, ic.Name, id_User, ic.ID_Status, "", id_Log, 1);

                if(errorMess != 1) errorList.Add(new ErrorItem("ErrorMessage", _db.ErrorMessages.Single(p => p.ID_ErrorMessage == errorMess && p.ID_Language == id_Lang).Description));
            }
            return errorList;
        }

        public static List<ErrorItem> Delete(int id_Lightbox, int id_User, int deep, int id_Lang)
        {
            List<ErrorItem> errorList = new List<ErrorItem>();
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();

            int errorMess = _db.DeleteLightbox(id_User, id_Lightbox, deep);

            if(errorMess != 1) errorList.Add(new ErrorItem("ErrorMessage", _db.ErrorMessages.Single(p => p.ID_ErrorMessage == errorMess && p.ID_Language == id_Lang).Description));

            return errorList;
        }
        #endregion

        #region Search

        private static String BuildQuery(Dictionary<String, System.Object> parametersList, Dictionary<String, bool> exactList)
        {
            String query = "";

            DateTime noValue = new DateTime(2000, 1, 1);

            foreach (KeyValuePair<String, System.Object> kvp in parametersList)
            {
                if (kvp.Key == "ID_Email" || kvp.Key == "ID_Status")
                {
                    if (int.Parse(kvp.Value.ToString()) >= 1)
                        query += kvp.Key + " = " + int.Parse(kvp.Value.ToString());
                }
                else if (kvp.Key == "ID_ReceivedEmail")
                {
                    if (exactList[kvp.Key])
                    {
                        if (kvp.Value.ToString() != "")
                            query += kvp.Key + " = '" + kvp.Value.ToString() + "'";
                    }
                    else
                    {
                        if (kvp.Value.ToString() != "")
                            query += kvp.Key + " LIKE '%" + kvp.Value.ToString() + "%'";
                    }
                }
                else if (kvp.Key == "CreatedTime")
                {
                    if (DateTime.Parse(kvp.Value.ToString()) != noValue)
                        query += "CreatedTime" + " = " + DateTime.Parse(kvp.Value.ToString()).ToString("yyyy-MM-dd") + "";
                }
            }

            if (parametersList.ContainsKey("CreatedbeginDate") && parametersList.ContainsKey("CreatedendDate"))
            {
                query += " DATEDIFF(day, " + DateTime.Parse(parametersList["CreatedendDate"].ToString()).ToString("yyyy-MM-dd") + " , CreatedTime) >= 0";
                query += " DATEDIFF(day, CreatedTime , " + DateTime.Parse(parametersList["CreatedbeginDate"].ToString()).ToString("yyyy-MM-dd") + ") >= 0";
            }

            return query;
        }

        public static System.Object GetLightbox(Dictionary<String, System.Object> parametersList, Dictionary<String, bool> exactList, bool oneRow)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            String query = BuildQuery(parametersList, exactList);
            var v = _db.ExecuteQuery<Lightbox>(query);
            if (oneRow)
            {
                if (v.Count() == 0)
                    return null;
                return v.First();
            }
            if (v.Count() == 0)
                return null;
            return v.ToList();
        }

        #endregion
    }
}